package com.pinyoumei.app.utils;

import android.app.Activity;
import android.os.AsyncTask;
import android.widget.Toast;

import com.pinyoumei.app.bean.WxPayEntity;
import com.tencent.mm.opensdk.constants.Build;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;


/**
 * Created by Administrator on 2020-05-19.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2020-05-19        Administrator             新增Person类
 * 2020-05-19        Administrator             增加sex属性
 */
public class PayUtils {
    //app id
    private  final String WECHAT_APP_ID ;
    //商户id
    private  final String PARENT_ID ;

    private static final int SDK_PAY_FLAG = 1;
    private IWXAPI api;
    private Activity activity;

    public PayUtils(IWXAPI api, Activity activity , String PARENT_ID) {
        this.activity = activity;
        this.api = api;
        this.PARENT_ID = PARENT_ID;
        this.WECHAT_APP_ID = SPUtils.getWxKey(activity);
    }
    /**
     * 微信支付
     */
    public void payByWechat(final WxPayEntity wxPayEntity) {
        if (api.isWXAppInstalled() && api.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT) {
        } else {
            // mView.showToast("未安装微信或微信版本过低！", R.mipmap.icon_fail);
            Toast.makeText(activity, "未安装微信或微信版本过低", Toast.LENGTH_SHORT).show();
            return;
        }
        if (null == wxPayEntity)
            return;
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                //  LogUtils.LOG_D(getClass(), "正在生成订单，请稍等.");
                PayReq request = new PayReq();
                //
                request.appId = WECHAT_APP_ID;
                request.partnerId = PARENT_ID;
                // 后台返回
                request.prepayId = wxPayEntity.getPrepay_id();
                request.packageValue = wxPayEntity.getPackageName();
                request.nonceStr = wxPayEntity.getNoncestr();
                request.timeStamp = wxPayEntity.getTimestamp();
                request.sign = wxPayEntity.getSign();
                  LogUtils.e( "正常调起支付.");
                boolean sendReq = api.sendReq(request);
                 LogUtils.e( "支付请求发送-----" + sendReq);

                return null;
            }
        }.execute();

    }


}
